/**
* @mxmlc -output=bin/assets/modules/menu.swf -external-library-path+=lib/library.swc -debug=false -noplay
*/
package menu 
{
	import flash.display.DisplayObject;
	import menu.view.FightMenu;
	import menu.view.GeneralMenu;
	import menu.view.NumberMenu;
	import menu.view.OrdinaryMenu;
	import org.airmvc.View;
	
	/**
	 * ...
	 * @author WLDragon 2014-03-15
	 */
	public class MenuV extends View
	{
		
		public function MenuV(){}
		
		override public function init():void 
		{	
			this.mouseEnabled = false;
			
			ordinaryMenu = new OrdinaryMenu();
			ordinaryMenu.visible = false;
			generalMenu = new GeneralMenu();
			generalMenu.visible = false;
			fightMenu = new FightMenu();
			fightMenu.visible = false;
			numberMenu = new NumberMenu();
			numberMenu.visible = false;
			
			addChild(ordinaryMenu);
			addChild(generalMenu);
			addChild(fightMenu);
			addChild(numberMenu);
			
			curMenu = null;
			
			//菜单使用比较频繁，不需要从显示列表中移除，不显示时用visible=false
			Lyr.ui.addChild(this);
		}
		
		override protected function addListeners():void 
		{
			receive(MenuC.UPDATE_ORDINARY, handleUpdateOrdinary);
			receive(MenuC.SELECT_GENERAL, handleSelectGeneral);
			receive(MenuC.SELECT_NUMBER, selectNumber);
		}
		
		private function selectNumber(data:Array,backMenu:int,tx:int,ty:int):void 
		{
			numberMenu.update(data,backMenu, tx, ty);
			switchMenu(numberMenu);
		}
		
		private function handleSelectGeneral(order:int,gs:Array):void 
		{
			generalMenu.update(order, gs);
			switchMenu(generalMenu);
		}
		
		/**
		 * 更新普通菜单
		 * @param	type     菜单类别
		 * @param	backType 它的上一级菜单
		 * @param	items    显示的列表项
		 * @param	px       描述
		 * @param	py
		 * @param	mx
		 * @param	my
		 */
		private function handleUpdateOrdinary(type:int,backType:int,items:Array,px:int = 125,py:int = 345,col:int = 0):void 
		{
			if (type == 0)
			{
				if(curMenu)
					curMenu.visible = false;
				
				curMenu = null;
			}
			else
			{
				switchMenu(ordinaryMenu);
				Kyb.setOperater(ordinaryMenu);
				ordinaryMenu.update(type,backType, items, px, py, col);
			}
		}
		
		private function switchMenu(menu:DisplayObject):void
		{
			if (curMenu != menu)
			{
				if(curMenu)
					curMenu.visible = false;
					
				curMenu = menu;
				curMenu.visible = true;
			}
		}
		
		private var ordinaryMenu:OrdinaryMenu;
		private var generalMenu:GeneralMenu;
		private var fightMenu:FightMenu;
		private var numberMenu:NumberMenu;
		private var curMenu:DisplayObject;
	}
	
}